package com.blb.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.entity.Department;
import com.blb.entity.Post;
import com.blb.mapper.DepartmentMapper;
import com.blb.mapper.PostMapper;
import com.blb.mapper.StaffMapper;
import com.blb.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.service.IStaffService;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private PostMapper postMapper;

    public List<Department> getAllDepartment(String cId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("c_id",cId);
        return departmentMapper.selectByMap(map);
    }

    @Override
    public List<Department> getDepartmentByPid(String pid) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pid",pid);
        return departmentMapper.selectByMap(map);
    }

    /**
     * 根据pid查询所有父部门
     * @param pid
     * @return
     */
    @Override
    public List<Department> getDepartmentsByPid(String pid, String cId) {
        return departmentMapper.getDepartmentsByPid(pid,cId);
    }

    @Override
    public List<Department> getDepartmentsById(String dId) {
        return departmentMapper.getDepartmentsById(dId);
    }

    @Override
    public Department getDepartmentById(String id) {
        Department department = departmentMapper.selectById(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("d_id",id);
        List<Post> posts = postMapper.selectByMap(map);
        department.setPosts(posts);
        return department;
    }

    @Override
    public void updateDepartment(Department department) {
        //更新部门信息
        departmentMapper.updateById(department);
        List<Post> postList = department.getPosts();
        List<String> list = new ArrayList<String>();
        //更新部门下的职位
        for(Post post : postList){
            //如果id为空，就添加，否则就修改
            if(StringUtils.isEmptyOrWhitespace(post.getId())){
                post.setId(UUIDUtils.getId());
                post.setDId(department.getId());
                postMapper.insert(post);
            }else{
                postMapper.updateById(post);
            }
            list.add(post.getId());
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("d_id",department.getId());
        List<Post> posts = postMapper.selectByMap(map);
        //循环判断当传过来的数据中没有当前id则删除
        for (Post p : posts) {
            String id = p.getId();
            if(!list.contains(id)){
                postMapper.deleteById(id);
                staffMapper.updateDepartmentId(id);
            }
        }
    }

    @Override
    public void insertDepartment(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void deleteDepartmentById(String id) {
        //删除部门
        departmentMapper.deleteById(id);
        staffMapper.updateDepartmentId(id);
        //删除职位
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("d_id",id);
        postMapper.deleteByMap(map);
    }

}
